package cevent.source.cloudcenter.system.controller.exception;/**
 * Created by Cevent on 2021/5/23.
 */

import cevent.source.cloudcenter.server.dto.ResponseDataDto;
import cevent.source.cloudcenter.server.exception.SystemException;
import cevent.source.cloudcenter.server.exception.ValidatorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author cevent
 * @description 系统模块异常捕获
 * @date 2021/5/23 9:36
 */
@ControllerAdvice
public class SystemControllerExceptionHandler {
    private static final Logger LOG= LoggerFactory.getLogger(SystemControllerExceptionHandler.class);

    @ExceptionHandler(value= ValidatorException.class)
    @ResponseBody
    public ResponseDataDto validatorExceptionHandler(ValidatorException validatorException){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setSuccess(false);
        responseData.setResponseMsg(validatorException.getMessage());
        LOG.warn(validatorException.getMessage());
        //前端通过非法途径发出请求，不给予真实的校验返回值
        //responseData.setResponseMsg("请求非法，请进入浏览器CEVENT-CMS系统后操作");
        return responseData;
    }

    @ExceptionHandler(value= SystemException.class)
    @ResponseBody
    public ResponseDataDto systemExceptionHandler(SystemException systemException){
        ResponseDataDto responseData=new ResponseDataDto();
        //设置返回的参数
        responseData.setSuccess(false);
        responseData.setResponseMsg(systemException.getMessage());
        //系统异常使用error，根据异常处理类返回响应异常描述
        LOG.info("系统异常，tree资源加载异常：{}",systemException.getSystemExceptionCode().getDesc());
        responseData.setResponseMsg(systemException.getSystemExceptionCode().getDesc());
        return responseData;
    }
}
